package protectjava;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

//通过 Servlet 读取 Cookie
/*
* 要读取 Cookie，您需要通过调用 HttpServletRequest
* 的 getCookies( )
* 方法创建一个 javax.servlet.http.Cookie 对象的数组。
* 然后循环遍历数组，并使用 getName() 和 getValue()
* 方法来访问每个 cookie 和关联的值。*/
@WebServlet(name = "ReadCookies ")
public class ReadCookies extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = null;
        Cookie[] cookies = null;
//        获取与该域相关的COOkie数组
        cookies = request.getCookies();

//        设置响应内容类型
        response.setContentType("text/html:charset=UTF-8");

        PrintWriter out = response.getWriter();
        String titile = "Delete Cookie Example";
        String docType = "<!DOCTYPE html>\n";
        out.print(docType+
                "<html><heard><title>"+titile+"</title></heard>" +
                "<body>\n");
        if(cookies == null){
            out.print("<h2>Cookie 名称和值</h2>");
            for (int i = 0 ; i < cookies.length ; i++){
                cookie = cookies[i];
                if (cookie.getName().compareTo("name") == 0){
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    out.print("已删除cookie："+
                            cookie.getName()+"<br/>"
                    );
                }
                out.print("名称："+cookie.getName());
                out.print("值：" + URLDecoder.decode(cookie.getValue(),
                        "utf-8")+"</br>");
            }
        }else{
            out.print(
                    "<h2 class=\"tutheader\">No Cookie founds</h2>"
            );
        }
        out.print("</body> \n </html>");
    }
}
